﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

using BusinessFacade;
using EntityManage;
using Microsoft.Reporting.WebForms;


public partial class Modules_Personal_RptTargetTypeReport : System.Web.UI.Page
{
    IPerf_Personal_EvaluateHandler evaluateHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_EvaluateDtlHandler evaluateDtlHandler = new Perf_Personal_EvaluateDtlManage();

    ISys_OrganizationHandler organizationHandler = new Sys_OrganizationManage();

    IPerf_Personal_TargetOwnerHandler targetOwnerHandler = new Perf_Personal_TargetOwnerManage();
    DataBase db = new DataBase();

    ISys_UserHandlerExt userHandlerExt = new Sys_UserManageExt();
    IPerf_Personal_EvaluateHandlerExt evaluateHandlerExt = new Perf_Personal_EvaluateManageExt();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();

    IPerf_Personal_ApprovalHandler erfApprovalHandler = new Perf_Personal_ApprovalManage();
    IReportHandler rptHandler = new ReportManage();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDDl();
            // PreviewReport();
        }
    }

    private void FillDDl()
    {
        DataTable dt = rptHandler.GetQuarterList();
        ddlTitle.DataSource = dt;
        ddlTitle.DataTextField = "Title";
        ddlTitle.DataValueField = "Title";
        ddlTitle.DataBind();
        ddlTitle.Items.Insert(0, new ListItem(string.Empty, string.Empty));
    }

    #region Preview Report
    private void PreviewReport()
    {
        string whereClause = " 1=1 ";
        SqlParameter[] parms = null;
        ProcessParms(ref whereClause, ref parms);

        rptViewer.LocalReport.ReportPath = "Modules/Personal/rptTargetType.rdlc";

        rptViewer.LocalReport.DataSources.Clear();

        DataTable dtNew = new DataTable();
        dtNew.Columns.Add("Name");
        dtNew.Columns.Add("Title");
        dtNew.Columns.Add("TotalScore", Type.GetType("System.Double"));
        dtNew.Columns.Add("TypeName");
       
        DataTable dtType = rptHandler.GetDistinctTargetType();

        if (whereClause != string.Empty)
        {
            whereClause += " and E.Target_User_ID=" + int.Parse(Session["CurrentUserID"].ToString());
        }

        DataTable dtTotalType=rptHandler.GetPerTargetTypeTotalGrade(whereClause,parms);

        if (dtType != null && dtType.Rows.Count > 0 && dtTotalType!=null && dtTotalType.Rows.Count>0)
        {
            foreach (DataRow r in dtType.Rows)
            {
                bool istrue = false;

                DataRow rNew = dtNew.NewRow();

                foreach (DataRow rt in dtTotalType.Rows)
                {
                    if (r["ID"].ToString() == rt["TypeID"].ToString())
                    {
                        rNew["Name"] = rt["TypeName"];
                        rNew["Title"] = string.Empty;
                        double totalScore = 0d;
                        double.TryParse(rt["TotalScore"].ToString(), out totalScore);
                        rNew["TotalScore"] = totalScore;
                        rNew["TypeName"] = rt["TypeName"];
                        istrue = true;
                    }
                }
                if (!istrue)
                {
                    rNew["Name"] = r["Name"];
                    rNew["Title"] = string.Empty;                    
                    rNew["TotalScore"] = 0;
                    rNew["TypeName"] = r["Name"];
                }

                dtNew.Rows.Add(rNew);
            }
        }

        if (dtNew != null && dtNew.Rows.Count > 0)
        {
            rptViewer.Visible = true;
            lblNoRecord.Visible = false;
            ReportDataSource datasource = new ReportDataSource("rptDsTargetsType_DataTable1", dtNew);
            rptViewer.LocalReport.DataSources.Add(datasource);

            ReportParameter[] parameters = new ReportParameter[4];
            parameters[0] = new ReportParameter("DateFrom", DateTime.Now.ToShortDateString());
            parameters[1] = new ReportParameter("DateTo", DateTime.Now.ToShortDateString());
            parameters[2] = new ReportParameter("ReportTitle", "个人指标类型得分成绩统计");
            parameters[3] = new ReportParameter("RptName", "指标类型总得分");
            rptViewer.LocalReport.SetParameters(parameters);
            rptViewer.LocalReport.Refresh();
        }
        else
        {
            rptViewer.Visible = false;
            lblNoRecord.Visible = true;
        }
    }
           
    
    #endregion


    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();


        if (ViewState["Title"] != null && ViewState["Title"].ToString()!=string.Empty)
        {
            whereClause += " and Title =@Title ";
            lstParm.Add(new SqlParameter("@Title", ViewState["Title"].ToString()));
        }

        parms = lstParm.ToArray();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["Title"] = ddlTitle.SelectedValue;

        PreviewReport();
    }
}
